package com.hp.bon.sgw.start;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.security.config.SecurityConfigManager;
import com.hp.bon.sgw.bean.SysPertriNetBean;
import com.hp.bon.sgw.core.ChannelServer;
import com.hp.bon.sgw.ctrl.SGWProgramerStarter;
import com.hp.bon.sgw.ctrl.SpringProgramerStarter;
import com.hp.bon.sgw.service.ConfigService;
import com.hp.bon.sgw.snmp.SgwNMContext;
import com.hp.bon.sgw.threads.SecurityConfigThread;
import com.hp.bon.sgw.threads.TaskManager;
import com.hp.bon.sgw.util.LogTool;

/**
 * @author wanghan
 * 
 */
public class SGWServerStarter {
	private static final Logger logger = LoggerFactory.getLogger(SGWServerStarter.class);

	private volatile boolean stopFlag = false;

	public void stop() {
		stopFlag = true;
	}

	public void start(String[] args) {

		// RunLogWriterRunnable._LogBasename = "mdrapi_pxy";
		// RunLog._Module = "SGW";

		LogTool.installSLF4JBridge();
		logger.info("start SGWServerStarter...");
		String propFile = null;
		if (args.length > 0) {
			propFile = args[0];
		} else {
			propFile = "etc/sgw.properties";
		}

		// LogTool.loadProperty(propFile);//在调用SpringProgramer调用过一遍了
		String[] springXmls = new String[] { "spring-base.xml", "spring-sgwserver.xml", "spring-province.xml"};
		SpringProgramerStarter starter = new SGWProgramerStarter();
		starter.setStartPropertyFile(propFile);
		starter.setSpringFiles(springXmls);
		starter.start();

		SpringContext.setCtx(starter.ctx);
		SpringContext.getInstance().getPertriBean().setNewStatus(SysPertriNetBean.PETRI_STATUS_ONLINE_ONNET);
		//SpringContext.getInstance().getPertriBean().setNewStatus(SysPertriNetBean.PETRI_STATUS_ONLINE_OFFNET);

		// Rest service
    try
    {
      SgwNMContext.init();
    } catch (Exception e) {
      logger.warn("init NMContext error " + e.fillInStackTrace());
    }
		// snmp trap
		// try {
		// SgwNMContext.init();
		// } catch (Exception e) {
		// logger.warn("init NMContext error " + e.fillInStackTrace());
		// }
		// 初始化安全框架配置
		try {
			SecurityConfigManager.getInstance().init(ConfigService.sgwHostId);
		} catch (Exception e) {
			logger.warn("init SecurityConfigManager error " + e.fillInStackTrace());
		}
		// 启动任务线程
		TaskManager.startThreads();
		// 启动channel
		ChannelServer.getInstance().start();
		// 是否启动安全框架的线程
    String security_start = (String)System.getProperties().get("security_start");
    if ("true".equals(security_start)) {
      SecurityConfigThread.getInstance().start();
    }

		while (!stopFlag && !starter.needStop()) {
			try {
				Thread.sleep(500);
				starter.runPeriodJob();
			} catch (InterruptedException e) {
				break;
			}
		}
		// 停止channel
		ChannelServer.getInstance().stop();
		// 停止线程，等待1000毫秒
		TaskManager.stopThreads(1000);
		System.exit(0);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SGWServerStarter start = new SGWServerStarter();
		start.start(args);
	}
}